package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.constants.CommonConstant;
import com.dmcb.common.business.utils.WebUtil;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.crawlers.article.QianBaoParser;
import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.entities.Author;
import com.dmcb.trade.business.mappers.ArticleCrawlMapper;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 钱报文章全部导入
 * Created by 黑米 on 2017/3/9.
 */
@Service
public class QianBaoCrawler extends BaseCrawler {
    Logger logger = LoggerFactory.getLogger(QianBaoCrawler.class);

    @Autowired
    private QianBaoParser qianBaoParser;
    @Autowired
    private ArticleCrawlMapper articleCrawlMapper;

    @Override
    protected String url() {
        return CrawlConstant.QIANBAO_URL;
    }

    @Override
    protected int tag() {
        return CrawlConstant.QIANBAO_TAG;
    }

    @Override
    protected String avatarSelect() {
        return "a.avatar>img";
    }

    @Override
    protected String channelNameSelect() {
        return "div.title>a";
    }

    /**
     * 频道名称是否同时是作者名称
     *
     * @return 是、否
     */
    @Override
    protected boolean isAuthorName() {
        return true;
    }

    @Override
    protected String listSelect() {
        return "div.piece";
    }

    @Override
    protected String titleSelect() {
        return "div.piece>a";
    }

    @Override
    protected String authorLink() {
        return null;
    }

    @Override
    protected String authorSelect() {
        return "div.title>a";
    }

    private Document getDocument(String src, int page, int type, Map<String, String> map) {
        String json = WebUtil.sendPost(src, "page=" + page + "&category=" + type, map);
        if (StringUtils.isBlank(json)) {
            return null;
        }
        json = json.replace("\\\"", "\"").replace("\\\n", "").replace("\\/", "/");
        return Jsoup.parse(json);
    }

    @Override
    protected JsonResult crawl(int authorId, String link, Author author, Document doc, boolean all) {
        // 爬取文章
        int page = 1;
        int totalAll = 0;
        String pic = "http://www.qjwb.com.cn/ajax/loadmorepicnews";
        String load = "http://www.qjwb.com.cn/ajax/loadmorenews";
        //post请求是需要加入以下信息
        Map<String, String> map = new HashMap<String, String>();
        map.put("X-Requested-With", "XMLHttpRequest");
        int[] types = new int[]{2, 3, 4, 5};
        for (int type : types) {
            //当类别为5（图片）时，加载请求的链接为pic
            load = type == 5 ? pic : load;
            doc = getDocument(load, page, type, map);
            while (page <= CrawlConstant.CRAWL_PAGE_TOTAL) {
                if(doc == null){
                    return error(CrawlConstant.MSG_WARN_NO_CRAWL_ARTICLE);
                }
                JsonResult result = iterate(authorId, doc);
                if (!result.isSuccess()) {
                    return result;
                }
                totalAll += result.getInt();
                if (!all && result.getInt() < 1) {
                    break;
                }
                page++;
                doc = getDocument(load, page, type, map);
            }
        }

        return totalAll == 0 ? error(CrawlConstant.MSG_WARN_NO_UPDATE) : success(totalAll);
    }

    /**
     * 迭代解析、保存文章
     *
     * @return 成功保存数量
     */
    protected JsonResult iterate(int authorId, Document doc, String listSelect, String titleSelect) {

        Elements elements = doc.select(listSelect);
        if (elements.size() == 0) {
            return success(0);
        }
        int succ = 0;
        int contNot = 0;
        for (Element element : elements) {
            // 获取标题、链接元素
            Element ele = element.select(titleSelect).first();
            if (ele != null) {
                try {
                    //校验文章是否标有原创标识
                    if(StringUtils.isBlank(ele.select("span.original").text())){
                        contNot++;
                        if(contNot >= 10){
                            return success(1);
                        }
                        continue;
                    }
                    // 校验作者、标题是否已存在
                    String title = ele.select("div.title").text().trim();
                    Article article = articleCrawlMapper.queryByAuthorTitle(authorId,WebUtil.URLDecode(title) );
                    if (article != null) {
                        continue;
                    }
                    String href = ele.attr("href");
                    JsonResult result = parse(authorId, href);
                    succ += result.isSuccess() ? 1 : 0;
                } catch (Exception e) {
                    logger.error("Parser.parse error,{}", ele, e);
                    return error(CommonConstant.ABNORMAL_MSG);
                }
            }
        }
        return success(succ);
    }


    @Override
    protected JsonResult parse(int authorId, String link) throws Exception {
        return qianBaoParser.parse(authorId, link);
    }

    @Override
    protected String getBody(Document doc) {
        return qianBaoParser.getBody(doc);
    }
}
